package com.ocom.human.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.master.EntityMngUserInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.human.dao.IDaoMngUserInfo;
import com.ocom.human.service.MngUserInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MngUserInfoServiceImpl extends ServiceImpl<IDaoMngUserInfo, EntityMngUserInfo> implements MngUserInfoService {

    @Autowired
    private IDaoMngUserInfo iDaoMngUserInfo;

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public PageInfo<EntityMngUserInfo> selectMngUserInfoList(JSONObject jsonObject) {
        QueryWrapper<EntityMngUserInfo> wrapper = new QueryWrapper<>();
        if (jsonObject.getInteger("comId")!=null){
            wrapper.eq("com_id",jsonObject.getInteger("comId"));
        }
        if (StringUtils.isNotEmpty(jsonObject.getString("userName"))){
            wrapper.eq("user_name",jsonObject.getString("userName"));
        }
        if (jsonObject.getInteger("role")!=null){
            wrapper.eq("role",jsonObject.getInteger("role"));
        }
        if (jsonObject.getJSONArray("roles")!=null && jsonObject.getJSONArray("roles").size()>0){
            wrapper.in("role",jsonObject.getJSONArray("roles"));
        }
        if (jsonObject.getInteger("regionId")!=null){
            wrapper.eq("region_id",jsonObject.getInteger("regionId"));
        }
        if (jsonObject.getInteger("areaId")!=null){
            wrapper.eq("area_id",jsonObject.getInteger("areaId"));
        }
        if (jsonObject.getInteger("deptId")!=null){
            wrapper.eq("dept_id",jsonObject.getInteger("deptId"));
        }
        if (jsonObject.getInteger("status")!=null){
            wrapper.eq("status",jsonObject.getInteger("status"));
        }
        wrapper.orderByDesc("update_time");
        PageHelper.startPage(jsonObject.getInteger("page"),jsonObject.getInteger("size"));
        List<EntityMngUserInfo> pointInfoPageInfo=this.list(wrapper);
        return new PageInfo<>(pointInfoPageInfo);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public EntityMngUserInfo selectUserName(String userName) {
        QueryWrapper<EntityMngUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        wrapper.last("limit 1");
        return iDaoMngUserInfo.selectOne(wrapper);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public boolean insertUserInfoService(EntityMngUserInfo userInfo) {
        return this.save(userInfo);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    @Override
    public boolean UpdateUserInfoService(EntityMngUserInfo userInfo) {
        return this.updateById(userInfo);
    }

    @DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public int deleteMngUserInfo(JSONObject jsonObject) {
        QueryWrapper<EntityMngUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("com_id", jsonObject.getInteger("comId"));
        wrapper.eq("id", jsonObject.getInteger("id"));
        return iDaoMngUserInfo.delete(wrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    public EntityMngUserInfo selectUserById(Integer userId) {
        QueryWrapper<EntityMngUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id", userId);
        return this.getOne(wrapper);
    }

}
